package com.springboot.demo.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.cx.item.common.exception.ParamsCheckException;
import com.cx.item.common.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.demo.mapper.GoodsMapper;
import com.springboot.demo.mapper.GoodsTypeMapper;
import com.springboot.demo.model.enums.GoodsTypeEnum;
import com.springboot.demo.model.example.GoodsExample;
import com.springboot.demo.model.example.GoodsTypeExample;
import com.springboot.demo.model.model.Goods;
import com.springboot.demo.model.model.GoodsType;
import com.springboot.demo.model.par.GoodsAddOrUpdatePar;
import com.springboot.demo.model.query.GoodsListQuery;
import com.springboot.demo.model.vo.GoodsDetailsVo;
import com.springboot.demo.model.vo.GoodsListVo;
import com.springboot.demo.model.vo.GoodsTypeListVo;
import com.springboot.demo.service.GoodsBGService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by hwm on 2018/6/21.
 */
@Service
public class GoodsBGServiceImpl extends BaseServiceImpl<Goods, GoodsExample, GoodsMapper> implements GoodsBGService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    @Override
    public PageInfo<List<GoodsListVo>> getGoodsList(GoodsListQuery goodsListQuery) {

        PageHelper.startPage(goodsListQuery.getPageNum(), goodsListQuery.getPageSize());
        List<GoodsListVo> goodsList = goodsMapper.getGoodsList(goodsListQuery);
        goodsList.stream().forEach(goodsListVo -> {
            goodsListVo.setGoodsTypeName(GoodsTypeEnum.getByValue(goodsListVo.getGoodsTypeCode()).getName());
        });

        return new PageInfo(goodsList);
    }

    @Override
    public GoodsDetailsVo getGoodsDetails(Integer goodsId) {

        GoodsExample goodsExa = new GoodsExample();
        GoodsExample.Criteria goodsExaCriteria = goodsExa.createCriteria();
        goodsExaCriteria.andGoodsIdEqualTo(goodsId);
        goodsExaCriteria.andGoodsStatusBetween(1, 2);
        List<Goods> goodsList = goodsMapper.selectByExampleWithBLOBs(goodsExa);

        if (CollUtil.isEmpty(goodsList)) {
            throw new ParamsCheckException("商品信息不存在");
        }

        GoodsDetailsVo goodsDetailsVo = new GoodsDetailsVo();
        BeanUtil.copyProperties(goodsList.get(0), goodsDetailsVo);
        goodsDetailsVo.setGoodsTypeName(GoodsTypeEnum.getByValue(goodsDetailsVo.getGoodsTypeCode()).getName());

        return goodsDetailsVo;
    }

    @Override
    public int updateGoods(GoodsAddOrUpdatePar goodsAddOrUpdatePar) {

        if (StrUtil.isNotEmpty(goodsAddOrUpdatePar.getGoodsTypeCode()) && !checkGoodsTypeIsExist(goodsAddOrUpdatePar.getGoodsTypeCode())) {
            throw new ParamsCheckException(StrUtil.format("商品类型{}不存在", goodsAddOrUpdatePar.getGoodsTypeCode()));
        }

        Goods goodsUp = new Goods();
        BeanUtil.copyProperties(goodsAddOrUpdatePar, goodsUp);
        // 富文本过滤
        if (StrUtil.isNotEmpty(goodsUp.getGoodsDetails())) {
            goodsUp.setGoodsDetails(goodsUp.getGoodsDetails());
        }
        goodsUp.setUpdateTime(new Date());

        return goodsMapper.updateByPrimaryKeySelective(goodsUp);
    }

    @Override
    public int addGoods(GoodsAddOrUpdatePar goodsAddOrUpdatePar) {

        if (!checkGoodsTypeIsExist(goodsAddOrUpdatePar.getGoodsTypeCode())) {
            throw new ParamsCheckException(StrUtil.format("商品类型{}不存在", goodsAddOrUpdatePar.getGoodsTypeCode()));
        }

        Goods goodsMod = new Goods();
        BeanUtil.copyProperties(goodsAddOrUpdatePar, goodsMod);
        // 富文本过滤
        if (StrUtil.isNotEmpty(goodsMod.getGoodsDetails())) {
            goodsMod.setGoodsDetails(goodsMod.getGoodsDetails());
        }

        goodsMod.setCreateTime(new Date());
        goodsMod.setGoodsStatus(1);

        goodsMapper.insertSelective(goodsMod);
        return goodsMod.getGoodsId();
    }

    /**
     * 商品类型是否存在
     *
     * @param goodsCode 存在true,不存在false
     * @return
     */
    private boolean checkGoodsTypeIsExist(String goodsCode) {

        if (StrUtil.isEmpty(goodsCode)) {
            return false;
        }

        GoodsTypeEnum byValue = GoodsTypeEnum.getByValue(goodsCode);
        return !(byValue == null);
    }

    @Override
    public List<Goods> selectByExampleWithBLOBs(GoodsExample example) {
        List<Goods> goodsList = goodsMapper.selectByExampleWithBLOBs(example);
        return goodsList;
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(Goods record) {
        return goodsMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public PageInfo<List<GoodsTypeListVo>> getGoodsTypeList(Integer goodsType) {

        GoodsTypeExample goodsTypeExam = new GoodsTypeExample();
        GoodsTypeExample.Criteria goodsTypeExamCriteria = goodsTypeExam.createCriteria();
        goodsTypeExamCriteria.andGoodsTypeStatusEqualTo(1);
        if (goodsType != null) {
            goodsTypeExamCriteria.andGoodsTypeEqualTo(goodsType);
        }
        List<GoodsType> goodsTypeList = goodsTypeMapper.selectByExample(goodsTypeExam);

        List<GoodsTypeListVo> goodsTypeListVoList = new ArrayList<>();
        for (GoodsType typepar : goodsTypeList) {
            GoodsTypeListVo vo = new GoodsTypeListVo();
            BeanUtil.copyProperties(typepar, vo);
            goodsTypeListVoList.add(vo);
        }
        return new PageInfo(goodsTypeListVoList);
    }

}
